import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/layout/Layout.vue'
import Login from '../views/login/Login.vue'

import local from '@/utils/local'

Vue.use(VueRouter)


// 路由
const routes = [
  //登录
  {
    visible: false,
    path: '/login',
    component: Login
  },
  // 后台首页
  {
    visible: true,
    path: '/',
    component: Layout,
    redirect: '/home',
    meta: { title: '后台首页', path: '/home', icon: 'icon-home'},
    children: [{
      path: '/home',
      component: () => import('@/views/home/Home.vue')
    }]
  },
]

// 动态路由
const dynamicRoutes=[
   //订单管理
   {
    visible: true,
    path: '/order',
    component: Layout,
    redirect: '/order/order-list',
    meta: { title: '订单管理', path: '/order',icon: 'icon-order-form'},
    children: [{
      path: '/order/order-list',
      component: () => import('@/views/order/OrderList.vue')
    },
    {
      path: '/order/order-edit',
      component: () => import('@/views/order/OrderEdit.vue')
    }
    ]
  },
  //商品管理
  {
    visible: true,
    path: '/goods',
    component: Layout,
    redirect: '/goods/goods.list',
    meta: { title: '商品管理', path: '/goods',icon: 'icon-commodity'},
    children: [{
      meta: { title: '商品列表', path: '/goods/goods-list'},
      path: '/goods/goods-list',
      component: () => import('@/views/goods/GoodsList.vue')
    },
    {
      meta: { title: '商品添加', path: '/goods/goods-add', roles: ['super'] },
      path: '/goods/goods-add',
      component: () => import('@/views/goods/GoodsAdd.vue')
    },
    {
      meta: { title: '商品分类', path: '/goods/goods-cate'},
      path: '/goods/goods-cate',
      component: () => import('@/views/goods/GoodsCate.vue')
    },
    ]
  },
  //店铺管理
  {
    visible: true,
    path: '/shop',
    component: Layout,
    meta: { title: '店铺管理', path: '/shop', roles: ['super'] ,icon: 'icon-dianpu'},
    children: [{
      path: '',
      component: () => import('@/views/shop/Shop.vue')
    }
    ],
  },
  // 账号管理
  {
    visible: true,
    path: '/account',
    component: Layout,
    redirect: '/account/account.list',
    meta: { title: '账号管理', path: '/account',icon: 'icon-account'},
    children: [{
      meta: { title: '账号添加', path: '/account/account-add', roles: ['super'] },
      path: '/account/account-add',
      component: () => import('@/views/account/AccountAdd.vue')
    },
    {
      meta: { title: '账号列表', path: '/account/account-list', roles: ['super'] },
      path: '/account/account-list',
      component: () => import('@/views/account/AccountList.vue')
    },
    {
      meta: { title: '修改密码', path: '/account/password-modify'},
      path: '/account/password-modify',
      component: () => import('@/views/account/PasswordModify.vue')
    },
    {
      meta: { title: '个人中心', path: '/account/Personal'},
      path: '/account/Personal',
      component: () => import('@/views/account/Personal.vue')
    },
    ]
  },
  // 销售统计
  {
    visible: true,
    path: '/total',
    component: Layout,
    redirect: '/total/goods-total',
    meta: { title: '销售统计', path: '/total',roles: ['super'],icon: 'icon-statistics'},
    children: [{
      meta: { title: '商品统计', path: '/total/goods-total'},
      path: '/total/goods-total',
      component: () => import('@/views/total/GoodsTotal.vue')
    },
    {
      meta: { title: '订单统计', path: '/total/order-total'},
      path: '/total/order-total',
      component: () => import('@/views/total/OrderTotal.vue')
    },
  
    ]
  },

]

const options404 = [
  {
    visible: false,
    path: '*',
    redirect: '/error404'
  },
  {
    visible: false,
    path: '/error404',
    component: () => import('@/views/error/Error404.vue')
  }
]





const router = new VueRouter({
  routes
})

/* 全局前置路由守卫 */
router.beforeEach((to, from, next) => {
  // 登录状态
  let isLogin = local.get('t_k') ? true : false;

  if (isLogin) {
    next() // 走你
  } else {
    // 如果去的是登录
    if (to.path === '/login') {
      next() // 走你
    } else {
      next({ path: '/login' }) // 如果去别的页面 就跳转到登录
    }
  }
})

// 判断有无权限
const hasPremission = (router, role) => {
  // 如果在meta中配置了 roles
  if (router.meta && router.meta.roles) {
    return router.meta.roles.includes(role) // 看roles是不是包含当前用户角色 如果包含 就是true 否则就是false
  } else {
    // 没有在meta中配置 roles 证明这个对象不需要做权限 都可以访问
    return true;
  }
}


// 计算有权限菜单
const calcMenus = (routes) => {
  let menus = routes.filter(v => v.visible)
  return menus;
}

/* 算出有权限的路由 */
const calcAccessRoutes=(dynamicRoutes,role)=>{
  let accessRoutes = dynamicRoutes.filter(v => {
    if (hasPremission(v, role)) {

      if (v.children && v.children.length) {
        // 递归
        v.children = calcAccessRoutes(v.children, role)
      }

      return true;
    } else {
      return false;
    }
  })

  return accessRoutes
}


/**
 * 计算动态路由
 */
 export const calcDynamicRoutes = () => {
   
  let role = local.get('role')

  if (!role) return;
 // 把有权限访问的路由算出来
 let accessRoutes = calcAccessRoutes(dynamicRoutes, role)
 let result = [...routes, ...accessRoutes];
console.log(result);
  // 计算有权限访问的菜单
  let menus = calcMenus(result)
  local.set('menus', menus)
  // 把算出来的加入路由实例对象中
  router.addRoutes(result)
  
}

calcDynamicRoutes(); // 刷新也调用一次计算动态路由方法


export default router
